var SUB_FORM = {
		init: function(){
			$('tr.r-required select.r-selected').each(function(){
				var $ob = $(this);
				var $tmp = $('#'+$ob.attr('data-get-id'));
				$ob.html('').append($tmp.html());
				$ob.val($ob.attr('data-select-id'));
				if($ob.hasClass('r-parent')){
					var $par = $ob.parent().parent();
					$.ajax({
						type: "POST",
						url: $ob.attr('data-url'),
						data: {
							id: $ob.val()
						},
						cache: false,
						success: function(data){
							var $js = $.parseJSON(data);
							var $select = $('select.'+$ob.attr('data-refer'), $par);
							if(parseInt($js.status, 10) == 1){
								$select.children('option[value!=-1]').remove();
								for(var i = 0; i < $js.data.length; i++){
									var str = '<option value="'+$js.data[i].id+'">'+$js.data[i].name+'</option>';
									$select.append(str).val($select.attr('data-select-id'));
									SUB_FORM.checkingRow($par);
								}
							}else{
								alert($js.data);
							}
							
						}
					});
				}
			});
			
			$('tr.r-required').each(function(){
				SUB_FORM.checkingRow($(this));
			});
			
			SUB_FORM.reInit();
			
			
		},
		checkingRow: function($row){
			$row.attr('data-count', '0');
			$('input.r-required, textarea.r-required', $row).each(function(){
				var $ob = $(this);
				if ($ob.val()!=$ob.attr('data-sample') && $ob.val()!=''){
					$row.attr('data-count', parseInt($row.attr('data-count'), 10) + 1);
				}
			});
			$('select.r-required', $row).each(function(){
				var $ob = $(this);
				if (parseInt($ob.val(), 10)>= 0){
					$row.attr('data-count', parseInt($row.attr('data-count'), 10) + 1);
				}
			});
		},
		add: function(){
			$('a.sub-form-add').unbind('click').click(function(){
				var $ob = $(this);
				switch ($ob.attr('data-action')){
				case 'education':
					var $tmp = $('#education-level-tmp');
					var $majorTmp = $('#education-major-addmore-tmp');
					var $count = parseInt($ob.attr('data-count'), 10);
					var $str = '<tr data-count="0" class="r-required">'
							+ '<td><input name="educations['+$count+'][date_from]" class="date-picker-month r-required date-lessthan" value=""/></td>'
							+ '<td><input name="educations['+$count+'][date_to]" class="date-picker-month r-required date-morethan" value=""/></td>'	
							+ '<td><input name="educations['+$count+'][education_school]" class="r-required" value=""></td>'
							+ '<td><input name="educations['+$count+'][major_subject]" class="r-required" value=""></td>'
							+ '<td>'
							+ '	<select name="educations['+$count+'][education_level_id]" data-get-id="education-level-tmp" data-select-id="-1" class="r-selected r-required education-level">'
							+ $tmp.html()
							+ '	</select>'
							+ $majorTmp.html()
							+ '</td>'
							+ '<td><input type="checkbox"  name="educations['+$count+'][is_checked]" value="1"/></td>'
							+ '<td><a href="#" class="remove-child" title="">&nbsp;</a></td>'
							+ '</tr>';
					$('#'+$ob.attr('data-refer-id')).append($str);
					$ob.attr('data-count', ++$count);
					break;
				case 'major':
					var $tmp = $('#major-tmp');
					var $majorTmp = $('#major-id-tmp');
					var $count = parseInt($ob.attr('data-count'), 10);
					var $str = '<tr data-count="0" class="r-required">'
							+ '<td>'
							+ '	<select name="majors['+$count+'][major_id]" data-get-id="major-tmp" data-select-id="-1" class="r-selected r-required major-select">'
							+ $tmp.html()
							+ '	</select>'
							+ $majorTmp.html()
							+ '<td><textarea name="majors['+$count+'][description]" class="mdescription"></textarea></td>'
							+ '<td><a href="#" class="remove-child" title="">&nbsp;</a></td>'
							+ '</tr>';
					$('#'+$ob.attr('data-refer-id')).append($str);
					$ob.attr('data-count', ++$count);
					break;
				case 'language':
					var $tmp = $('#language-tmp');
					var $tmp2 = $('option[value=-1]',$tmp).html();
					var $count = parseInt($ob.attr('data-count'), 10);
					var $str = '<tr data-count="0" class="r-required">'
							+ '<td>'
							+ '	<select name="languages['+$count+'][language_id]" data-get-id="language-tmp" data-select-id="-1" class="r-selected r-required r-parent" data-url="/admin/candidates/getLanguageLevel" data-refer="r-child">'
							+ $tmp.html()
							+ '	</select>'
							+ '</td>'
							+ '<td><select name="languages['+$count+'][language_level_id]" data-select-id="-1" class="r-required r-child"><option value="-1">'+$tmp2+'</option></select></td>'	
							+ '<td><textarea name="languages['+$count+'][description]" class="mdescription"></textarea></td>'
							+ '<td><input type="checkbox"  name="languages['+$count+'][is_checked]" value="1"/></td>'
							+ '<td><a href="#" class="remove-child" title="">&nbsp;</a></td>'
							+ '</tr>';
					$('#'+$ob.attr('data-refer-id')).append($str);
					$ob.attr('data-count', ++$count);
					break;
				case 'tech':
					var $tmp = $('#tech-tmp');
					var $tmp2 = $('#tech-level-tmp');
					var $tech_link = $('#tech-id-tmp');
					var $tech_level_link = $('#tech-level-id-tmp');
					var $count = parseInt($ob.attr('data-count'), 10);
					var $str = '<tr data-count="0" class="r-required">'
							+ '<td>'
							+ '	<select name="technologies['+$count+'][technology_id]" data-get-id="tech-tmp" data-select-id="-1" class="r-selected r-required">'
							+ $tmp.html()
							+ '	</select>'
							+ $tech_link.html()
							+ '</td>'
							+ '<td>'
							+ '	<select name="technologies['+$count+'][technology_level_id]" data-get-id="tech-level-tmp" data-select-id="-1" class="r-selected r-required">'
							+ $tmp2.html()
							+ '	</select>'
							+ $tech_link.html()
							+ '</td>'
							+ '<td><textarea name="technologies['+$count+'][description]" class="tdescription"></textarea></td>'
							+ '<td><input type="checkbox"  name="technologies['+$count+'][is_checked]" value="1"/></td>'
							+ '<td><a href="#" class="remove-child" title="">&nbsp;</a></td>'
							+ '</tr>';
					$('#'+$ob.attr('data-refer-id')).append($str);
					$ob.attr('data-count', ++$count);
					break;
				case 'skill':
					var $count = parseInt($ob.attr('data-count'), 10);
					var $str = '<tr data-count="0" class="r-required">'
							+ '<td>'
							+ '	<input name="skills['+$count+'][name]" class="r-required" value=""/>'
							+ '</td>'
							+ '<td><textarea name="skills['+$count+'][description]" class="mdescription"></textarea></td>'
							+ '<td><input type="checkbox"  name="skills['+$count+'][is_checked]" value="1"/></td>'
							+ '<td><a href="#" class="remove-child" title="">&nbsp;</a></td>'
							+ '</tr>';
					$('#'+$ob.attr('data-refer-id')).append($str);
					$ob.attr('data-count', ++$count);
					break;
				case 'working':
					var $tmp = $('#working-job-tmp');
					var $tmp2 = $('option[value=-1]',$tmp).html(); 
					var $addTmp = $('#working-job-id-tmp');
					var $addTmp2 = $('#working-pos-id-tmp');
					var $count = parseInt($ob.attr('data-count'), 10);
					var $str = '<tr data-count="0" class="r-required">'
							+ '<td><input name="workings['+$count+'][date_from]" class="date-picker-month r-required date-lessthan" value=""/></td>'
							+ '<td><input name="workings['+$count+'][date_to]" class="date-picker-month r-required date-morethan" value=""/></td>'	
							+ '<td><input name="workings['+$count+'][company_name]" class="r-required wcompany" value=""></td>'
							+ '<td>'
							+ '	<select name="workings['+$count+'][job_position_id]" data-get-id="working-job-tmp" data-select-id="-1" class="r-selected r-required working-job-select r-parent" data-url="/admin/candidates/getPosition" data-refer="r-child">'
							+ $tmp.html()
							+ '	</select>'
							//+ $addTmp.html()
							+ '</td>'
							+ '<td>'
							+ '	<select name="workings['+$count+'][position_id]" data-select-id="-1" class="r-required working-pos-select r-child">'
							+ '<option value="-1">'+$tmp2+'</option>'
							+ '	</select>'
							//+ $addTmp2.html()
							+ '<td><textarea name="workings['+$count+'][description]" class="wdescription"></textarea></td>'
							+ '</td>'
							+'<td><input type="checkbox" name="workings['+$count+'][is_confirmed]"  value="1"/></td>'
							+ '<td><a href="#confirm-info-'+$count+'" class="add-info" title="'+$('#confirm-title').text()+'"><img src="/css/admin/img/global/add-info.png"/></a><a href="#" class="remove-child" title="">&nbsp;</a>'
							+'<div class="hidden">'
							+'<div id="confirm-info-'+$count+'">'
							+'<table class="confirm-info"><tr><th>'+$('#confirm-person').text()+'</th>'
							+'<td><input name="workings['+$count+'][confirm_person]" 		class="wcperson"   value=""/></td>'
							+'</tr><tr><th>'+$('#confirm-phone').text()+'</th>'
							+'<td><input name="workings['+$count+'][confirm_phone_number]" 		class="wcphone number"  value=""/></td>'
							+'</tr><tr><th>'+$('#confirm-description').text()+'</th>'
							+'<td><input name="workings['+$count+'][confirm_description]" 		class="wcphone"  value=""/></td></tr></table>'
							+'</div></div></td>'
							+ '</tr>';
					$('#'+$ob.attr('data-refer-id')).append($str);
					$ob.attr('data-count', ++$count);
					break;
				case 'job-hope':
					var $count = parseInt($ob.attr('data-count'), 10);
					var $str = '<tr data-count="0" class="r-required">'
							+ '<td>'
							+ '	<input id="job_hopes_job_hope_'+$count+'" name="job_hopes['+$count+'][job_hope]" class="job-hope r-required autocomplete" data-source-url="/admin/candidates/ajaxGet?action_name=job_hope" value=""/><a href="#" class="autocomplete" data-related-id="job_hopes_job_hope_'+$count+'">▼</a>'
							+ '</td>'
							+ '<td><textarea name="job_hopes['+$count+'][description]" class="mdescription"></textarea></td>'
							+ '<td><a href="#" class="remove-child" title="">&nbsp;</a></td>'
							+ '</tr>';
					$('#'+$ob.attr('data-refer-id')).append($str);
					$ob.attr('data-count', ++$count);
					break;
				case 'remark-more':
					var $count = parseInt($ob.attr('data-count'), 10);
					var $str = '<tr data-count="0" class="r-required">'
							+ '<td>'
							+ '	<input name="remark_more['+$count+'][remarked_at]" class="remark-more r-required date-picker" value=""/>'
							+ '</td>'
							+ '<td><textarea name="remark_more['+$count+'][content]" class="mdescription"></textarea></td>'
							+ '<td><a href="#" class="remove-child" title="">&nbsp;</a></td>'
							+ '</tr>';
					$('#'+$ob.attr('data-refer-id')).append($str);
					$ob.attr('data-count', ++$count);
					break;
				}
				SUB_FORM.reInit();
				
				return false;
			});
		},
		
		reInit: function(){
			ADMIN_FORM.autocompleteRelated();
			$('input.date-picker').unbind('click').unbind('datepicker').datepicker();
			$('input.date-picker-month').datepicker({ 
				dateFormat: 'mm/yy',
				onClose: function(dateText, inst) { 
					var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
					var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
					$(this).datepicker('setDate', new Date(year, month, 1));
				},
				beforeShow : function(input, inst) {
					if ((datestr = $(this).val()).length > 0) {
						year = datestr.substring(3, datestr.length);
						month = datestr.substring(0, 2);
						$(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
						$(this).datepicker('setDate', new Date(year, month-1, 1));
					}
				}
			});
			$('input.number').unbind('keydown').keydown(function(event) {
				// Allow: backspace, delete, tab, escape, and enter,  Ctrl+A, home, end, left, right
				
				if ( event.keyCode ==190 
						|| event.keyCode == 110 
						|| event.keyCode == 46 
						|| event.keyCode == 8 
						|| event.keyCode == 9 
						|| event.keyCode == 27 
						|| event.keyCode == 13 
						|| (event.keyCode == 65 && event.ctrlKey === true) 
						|| (event.keyCode >= 35 && event.keyCode <= 39)) {
					return;
					//return false;
				}
				else {
					// Ensure that it is a number and stop the keypress
					if (event.shiftKey || ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 ))) {
						event.preventDefault(); 
					}
				}
			});
			$('tr.r-required input.r-required, tr.r-required textarea.r-required, tr.r-required select.r-required').unbind('change').change(function(){
				var $ob = $(this);
				if($ob.hasClass('r-parent')){
					var $par = $ob.parent().parent();
					$.ajax({
						type: "POST",
						url: $ob.attr('data-url'),
						data: {
							id: $ob.val()
						},
						cache: false,
						success: function(data){
							var $js = $.parseJSON(data);
							var $select = $('select.'+$ob.attr('data-refer'), $par);
							if(parseInt($js.status, 10) == 1){
								$select.children('option[value!=-1]').remove();
								for(var i = 0; i < $js.data.length; i++){
									var str = '<option value="'+$js.data[i].id+'">'+$js.data[i].name+'</option>';
									$select.append(str);
									SUB_FORM.checkingRow($par);
								}
							}else{
								alert($js.data);
							}
							
						}
					});
				}
				SUB_FORM.checkingRow($ob.parent().parent());
			});
			
			$('table.sub-form tr td > a.remove-child').unbind('click').click(function(){
				$(this).parent().parent().remove();
			});
			$('a.add-info').fancybox();
			$('a.addnew').unbind('click').click(function(){
				var $ob = $(this);
				$.fancybox.open({
					href : $ob.attr('data-url'),
					type : 'iframe',
					padding : 7,
					title: $ob.attr('data-title').toString().toUpperCase(),
					width: 1000,
					height: 700,
					helpers: {
						overlay:{
							closeClick : false
						}
					},
					afterClose: function(){
						$.ajax({
							type: "POST",
							url: $ob.attr('data-reload-url'),
							data: {
								
							},
							cache: false,
							success: function(data){
								$data = $.parseJSON(data);
								if(parseInt($data.status, 10)==1){
									if($data.hasCate && parseInt($data.hasCate, 10) == 1){
										$('select.'+ $ob.attr('data-refer')).each(function(){
											var $sel = $(this);
											var $bkval2 = $('option:selected', $sel).val();
											$sel.children('optgroup').remove();
											var $str = '';
											if($data.data.length){
												for(var i=0; i<$data.data.length; i++){
													$str+='<optgroup label="'+$data.data[i].name+'">';
													for(var j=0; j < $data.data[i].MasterMajors.length; j++){
														$str+='<option value="'+$data.data[i].MasterMajors[j].id+'">. . . '+$data.data[i].MasterMajors[j].name+'</option>';
													}
													$str+='</optgroup>';
												}
											}	
											$sel.append($str).val($bkval2);
										
										});
									}else{
										$('select.'+ $ob.attr('data-refer')).each(function(){
											var $sel = $(this);
											var $bkval2 = $sel.children('option:selected').val();
											$sel.children('option[value!=-1]').remove();
											var $str = '';
											if($data.data.length){
												for(var i=0; i<$data.data.length; i++){
													$str+='<option value="'+$data.data[i].id+'">'+$data.data[i].name+'</option>';
												}
											}
											
											$sel.append($str).val($bkval2);
										
										});
									}
									
								}else{
									alert('Error when trying connect to server. Contact with vo.quoc.viet@inte.co.jp');
								}
							}
						});
					}
				});
				return false;
			});
			/*$.ajax({
			type: "POST",
			url: '/admin/educations/getJson',
			data: {
				action_name: '_educationGetJson'
			},
			cache: false,
			success: function(data){
				var js = $.parseJSON(data);
				$au.autocomplete({
					source: js.school,
					delay: 200
				});
				//$lv.autocomplete({
				//	source: js.level,
				//	delay: 200
				//});
			}
		});*/
		},
		formValidate: function(){
			
			
			$('#form_main').submit(function(){
				var $isError = false;
				$('table.sub-form').each(function(){
					var $tbl = $(this);
					$('tr.r-required', $tbl).each(function(){
						var $ob = $(this);
						if($('.r-required', $ob).length > parseInt($ob.attr('data-count'), 10) && parseInt($ob.attr('data-count'), 10) > 0){
							$isError = true;
							alert($tbl.attr('data-error'));
							return false;
						}
						
							
						var $oDFrom = $('input.date-lessthan', $ob);
						var $oDTo = $('input.date-morethan', $ob);
						if($oDFrom.length){
							if($oDFrom.val()!='' && $oDFrom.val().length < 7){
								alert($tbl.attr('data-error-lessthan'));
								$isError = true;
								return false;
							};
							if($oDTo.val()!='' && $oDTo.val().length < 7){
								alert($tbl.attr('data-error-lessthan'));
								$isError = true;
								return false;
							};
							if($oDTo.val() != '' && $oDFrom.val() !=''){
								datestr = $oDFrom.val();
								var year = datestr.substring(3,datestr.length);
								var month = datestr.substring(0,2);
								var dFrom = new Date(year, month, 1);
								
								datestr = $oDTo.val();
								var year = datestr.substring(3,datestr.length);
								var month = datestr.substring(0,2);
								var dTo = new Date(year, month, 1);
								//var dFrom = parseInt($oDFrom.val(), 10);
								//var dTo = parseInt($oDTo.val(), 10);
								if(dFrom > dTo){
									alert($tbl.attr('data-error-lessthan'));
									$isError = true;
									return false;
								}
							}
						}
					});
					
				});
				return !$isError;
			});
			
		}
};

$(function(){
	SUB_FORM.init();
	SUB_FORM.add();
	SUB_FORM.formValidate();
	
});
/*-------------------------*/
var ADMIN_FORM = {
		isChecked: true,
		linkAction: '',
		autoShow: false,
		init:function(){
			$('table.table-form td.input span.error').hide();
			$('table.table-form td.input > input#button_submit').click(function(){
				/*
				 * check form require value
				 */
				$('input[data-require=not-empty], textarea[data-require=not-empty]').each(function(){
					if($(this).val()==''){
						ADMIN_FORM.isChecked = false;
						$(this).next('span.error').show();
					}else{
						$(this).next('span.error').hide();
					}
				});
				$('input[data-require=require-email]').each(function(){
					
					if($(this).val()=='' || !VALIDATOR.isEmail($(this).val())){
						ADMIN_FORM.isChecked = false;
						$(this).next('span.error').show();
					}else{
						$(this).next('span.error').hide();
					}
				});
				$('input[data-require=email]').each(function(){
					if($(this).val()==''){
						$(this).next('span.error').hide();
						return false;
					}
					if(!VALIDATOR.isEmail($(this).val())){
						ADMIN_FORM.isChecked = false;
						$(this).next('span.error').show();
					}else{
						$(this).next('span.error').hide();
					}
				});
				/*
				 * check form require selected
				 */
				$('select[data-require=selected]').each(function(){
					if(!$(this).val() || parseInt($(this).val(),10) < 0 ){
						ADMIN_FORM.isChecked = false;
						$('span.error', $(this).parent()).show();
					}else{
						$('span.error', $(this).parent()).hide();
					}
				});
				
				if(ADMIN_FORM.isChecked){
					$('#form_main').submit();
				}else{
					ADMIN_FORM.isChecked = true;
					alert($(this).attr('data-error'));
				}
				
			});
			ADMIN_FORM.autocompleteRelated();
			
			$('input.number').unbind('keydown').keydown(function(event) {
				// Allow: backspace, delete, tab, escape, and enter,  Ctrl+A, home, end, left, right
				
				if ( event.keyCode ==190 
						|| event.keyCode == 110 
						|| event.keyCode == 46 
						|| event.keyCode == 8 
						|| event.keyCode == 9 
						|| event.keyCode == 27 
						|| event.keyCode == 13 
						|| (event.keyCode == 65 && event.ctrlKey === true) 
						|| (event.keyCode >= 35 && event.keyCode <= 39)) {
					return;
					//return false;
				}
				else {
					// Ensure that it is a number and stop the keypress
					if (event.shiftKey || ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 ))) {
						event.preventDefault(); 
					}
				}
			});
			$('select.refer-to').change(function(){
				$('#'+$(this).attr('data-refer')).val($(this).val());
			});
			$('input[type=checkbox].multi.refer-to').click(function(){
				var sepa = $(this).attr('data-separator');
				var obj = $('#'+$(this).attr('data-refer'));
				var val = $(this).attr('data-add-first') + $(this).val() + $(this).attr('data-add-last');
				if($(this).is(':checked')){
					if(obj.val()==''){
						obj.val(sepa);
					}
					var str = obj.val() + val + sepa;
					obj.val(str);
				}else{
					var str = obj.val()+'';
					str = str.replace(sepa + val + sepa+'',sepa);
					obj.val(str);
					if(obj.val()==sepa){
						obj.val('');
					}
				}
			});
			ADMIN_FORM.initSelectParent();
		},
		autocompleteRelated: function(){
			$('input.autocomplete').each(function(){
				var $ob = $(this);
				$.ajax({
					type: "POST",
					url: $ob.attr('data-source-url'),
					data: {
						
					},
					cache: false,
					success: function(data){
						$data =$.parseJSON(data);
						if($data.data){
							$ob.unbind('autocomplete').autocomplete({
								'source': $data.data,
								'minLength': 0,
								'delay': 100
							});
						}
						
					}
				});
				
				
			}).unbind('focus').focus(function(){
				//$(this).autocomplete('search',$(this).val());
			});
			
			$('a.autocomplete').unbind('click').click(function(){
				var $ob = $(this);
				var $in = $('#'+$ob.attr('data-related-id'));
				$in.autocomplete('search', $in.val());
				//$in.autocomplete('search', '');
				ADMIN_FORM.autoShow = true;
				return false;
			});
			$('body').unbind('click').click(function(){
				if(ADMIN_FORM.autoShow){
					$('ul.ui-autocomplete').hide();
					ADMIN_FORM.autoShow = false;
				}
			});
		},
		initSelectParent:function(){
			$('select.select-parent').each(function(){
				var obj = $(this);
				var id = obj.attr('data-child-id');
				var urlUpdate = obj.attr('data-update');
				var require = obj.attr('data-child-require');
				var it = obj.attr('data-child-select');
				var cls =  obj.attr('data-child-class');
				var more =  obj.attr('data-child-more');
				if(id && urlUpdate){
					var chl = $('#'+id);
					var str = '<select name="tmp_'+id+'" id="tmp_'+id+'" data-require="'+require+'" class="'+cls+'" '+more+'>'
							+ '<option value="-1">'+it+'</option></select>';
					chl.parent().append(str);
					obj.change(function(){
						$('#'+id).val('-1');
						$.ajax({
							type: "POST",
							url: urlUpdate,
							data: {
								cate_id: $(this).val()
							},
							cache: false,
							success: function(data){
								data = $.parseJSON(data);
								if(data.status==1){
									$('select#tmp_'+id).html('<option value="-1">...loading...</option>');
									var str='<option value="-1" selected>'+it+'</option>';
									if(data.data.length){
										for(var i=0; i<data.data.length; i++){
											str+='<option value="'+data.data[i].id+'">'+data.data[i].name+'</option>';
										}
									}
									$('select#tmp_'+id).html('');
									$('select#tmp_'+id).append(str);
								}else{
									alert('Error when trying connect to server. Contact with vcqviet@gmail.com');
								}
							}
						});
					});
				}
				if(parseInt(obj.val(),10)>0){
					$.ajax({
						type: "POST",
						url: urlUpdate,
						data: {
							cate_id: obj.val()
						},
						cache: false,
						success: function(data){
							data = $.parseJSON(data);
							if(data.status==1){
								$('select#tmp_'+id).html('<option value="-1">...loading...</option>');
								var str='<option value="-1" selected>'+it+'</option>';
								
								if(data.data.length){
									for(var i=0; i<data.data.length; i++){
										str+='<option value="'+data.data[i].id+'" '+(parseInt($('#'+id).val(),10)==parseInt(data.data[i].id,10)?'selected':'')+'>'+data.data[i].name+'</option>';
									}
								}
								$('select#tmp_'+id).html('');
								$('select#tmp_'+id).append(str);
							}else{
								alert('Error when trying connect to server. Contact with vcqviet@gmail.com');
							}
						}
					});
					
				}
				$('select#tmp_'+id).change(function(){
					$('#'+id).val($(this).val());
				});
			});
		},
		listLinked:function(val,val_links){
			dataString = 'type_id='+val;
			$.ajax({
				type: "POST",
				url: ADMIN_FORM.linkAction,
				data: dataString,
				cache: false,
				success: function(msg){
					var sel = document.getElementById('AmountLinks_linked_id');
					sel.options.length = 0;
					var data = $.parseJSON(msg);
					for(var i=0;i<data.data.length;i++){
						var option_s=null;
						if(parseInt(data.data[i].id) == parseInt(val_links)){
							option_s = new Option(data.data[i].name,data.data[i].id,true,true);
						}else{
							option_s = new Option(data.data[i].name,data.data[i].id);
						}
						sel.options.add(option_s);
					}
				}
			});
		}
}